<template>
    <div style="height: 562px;">
        <div style="padding: 1px 0">
          <el-input
            style="width: 250px; font-size:medium"
            prefix-icon="el-icon-present"
            placeholder="请输入订单号"
            v-model="number"
            size="medium"
            clearable
          ></el-input>
          <el-button round size="medium" class="ml-5" type="primary" @click="load" style="font-size:medium; border-radius: 10px; margin-top: 10px; margin-bottom: 4px">搜索</el-button>
          <el-button round class="ml-5" type="info" @click="reset" style="font-size:medium;margin-left: 7px" size="medium">重置</el-button>
        </div>

        <div style="padding: 5px 0">
          <!-- <el-button class="el-icon-circle-plus-outline" type="primary" @click="handleAdd" style="font-size:medium">添加</el-button> -->
          <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要批量删除吗?"
                @confirm="handleDeleteBatch">
          <el-button type="danger" slot="reference" style="font-size:medium">批量取消订单</el-button>
        </el-popconfirm>
          <!-- <el-button class="ml-5" type="primary" @click="exp" style="font-size:medium">导出</el-button> -->
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg5'" @selection-change="handleSelectionChange" style="font-size:large;">
          <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column
            prop="orderCode"
            label="订单号"
            width="260"
            align="center"
          ></el-table-column>
          <el-table-column prop="status" label="订单状态" width="120" align="center">
            <template v-slot="scope">
              <div v-if="scope.row.status === '待付款'">
                <el-tag type="warning" size="medium"><span style="font-size:18px; font-weight:600">{{ scope.row.status }}</span></el-tag>
              </div>
              <div v-else>
                <el-tag type="success" size="medium"><span style="font-size:18px; font-weight:600">{{ scope.row.status }}</span></el-tag>
              </div>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="userId"
            label="下单用户"
            width="85"
            align="center"
          ></el-table-column> -->
          <!-- <el-table-column
            prop="address"
            label="地址"
            width="80"
            align="center"
          ></el-table-column> -->
          <el-table-column label="实收金额" width="100" align="center" >
            <template v-slot="scope">
              {{ scope.row.totalPrice }}.00
            </template>
          </el-table-column>
          <el-table-column
            prop="orderTime"
            label="下单时间"
            width="219"
            align="center"
          ></el-table-column>
          <!-- <el-table-column
            prop="checkoutTime"
            label="结账时间"
            width="203"
            align="center"
          ></el-table-column> -->
          <el-table-column label="查看商品" width="140" align="center">
            <template v-slot="scope">
              <el-button type="primary" @click="viewFlowers(scope.row.id)"><span style="font-size:15px">查看商品</span></el-button>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="remark"
            label="备注"
            width="83"
            align="center"
          ></el-table-column> -->
          <el-table-column label="" width="220" align="center">
            <template slot-scope="scope">
              <el-button :disabled="scope.row.status === '已支付'" @click="pay(scope.row)" type="success" slot="reference"> <span style="font-size:16px">付 款</span></el-button>
              <el-button :disabled="scope.row.status === '已支付'" type="warning" slot="reference" @click="cancel(scope.row.id)"><span style="font-size:16px">取 消</span></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- <div style="padding: 10px 0">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[7, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div> -->


        <el-dialog title="商品信息" :visible.sync="dialogFormVisible1" width="45%" style="">
          <el-table :data="flowersData" border stripe :header-cell-class-name="'headerBg2'" @selection-change="handleSelectionChange" style="font-size:medium; background-color:aliceblue">
          <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column label="鲜花图片" prop="img" width="185" height="110" align="center">
            <template slot-scope="scope">
              <el-image :src="scope.row.img" :preview-src-list="[scope.row.img]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="鲜花名称" width="128" align="center"></el-table-column>
          <el-table-column prop="price" label="鲜花价格" width="118" align="center">
            <template v-slot="scope">
              {{ scope.row.price }}.00
            </template>
          </el-table-column>
          <!-- <el-table-column prop="code" label="商品码" width="160" align="center"></el-table-column> -->
          <!-- <el-table-column prop="targets" label="使用对象" width="120" align="center"></el-table-column> -->
          <el-table-column prop="num" label="购买数量" width="120" align="center"></el-table-column>
          </el-table>
        </el-dialog>


    </div>
</template>

<script>
export default {
  name: 'Orders',
  data(){
    return{
        tableData: [],
        flowersData: [],
        total: 0,
        pageNum: 1,
        pageSize: 7,
        number: "",
        user: "",
        dialogFormVisible: false,
        dialogFormVisible1: false,
        multipleSelection: [],
        form:{},
        isEdit: false,
    }
  },
  created(){
    this.load();
  },
  methods:{
    load() {
        this.request.get("/orders/page",{
          params :{
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            number: this.number,
            user: this.user
          }
        }).then(res =>{
          this.tableData = res.records
          this.total = res.total
        })
      },
      pay(row){
        this.$confirm('', '是否确定付款', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
        }).then(() => {
          window.open('http://8.138.58.18:7777/alipay/pay?subject=' + '鲜花支付订单' + '&traceNo=' + row.orderCode + '&totalAmount=' + row.totalPrice)
          // this.$notify.success('付款成功！')
          // this.$router.push('/front/orders')
          // this.load();
        });
      },
    viewFlowers(orderId){
      console.log('orderId',orderId);
      this.request.get("/orders/getFlowersById/" + orderId).then(res =>{
        this.flowersData = res.data
        this.dialogFormVisible1 = true
      })
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    reset() {
       this.number = "",
       this.user = "",
       this.load()
    },
    handleAdd(){
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row){
      console.log('row',row);
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.isEdit = true
    },
    handleDel(id){
      console.log('id',id);
      this.request.delete("/orders/delete/"+id).then(res =>{
        if(res){
          this.$message.success('删除订单成功!')
          this.load()
        }else{
          this.$message.error('删除订单失败!')
          this.load()
        }
      })
    },
    handleDeleteBatch(){
      let ids = this.multipleSelection.map(v => v.id)
      console.log('ids',ids)
      if(ids.length === 0){
        this.$message.warning('批量删除前,请先勾选若干订单')
      }else{
        this.request.delete("/orders/deleteBatch/",{data:ids}).then(res =>{
          if(res.code === '200'){
            this.$message.success('批量删除订单成功!')
            this.load()
          }else{
            this.$message.error('批量删除订单失败!')
            this.load()
          }
        })
      }
    },
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    save(){
      if(this.isEdit === false){
        console.log('add');
        this.request.post("/orders/save",this.form).then(res =>{
        if(res){
          this.$message.success('添加订单成功!')
          this.dialogFormVisible = false
          this.load()
        }else{
          this.$message.error('添加订单失败!')
          this.dialogFormVisible = false
          this.load()
        }
      })
      }else{
        console.log('edit');
        this.request.put("/orders/edit",this.form).then((res) =>{
          if(res){
            this.$message.success("修改订单信息成功!")
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }else{
            this.$message.error('修改订单信息失败!')
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }
        })
      }
    },
    cancel(orderId){
      console.log('买买买',orderId);
      this.$confirm('', '是否确定取消订单', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.request.delete('/orders/delete/'+orderId).then(res =>{
            if(res.code === '200'){
              this.$notify({
                type: 'success',
                message: '取消订单成功!'
              });
              this.dialogFormVisible = false
              this.load()
            }else{
              this.$notify.warning('取消订单失败!')
              this.dialogFormVisible = false
              this.load()
            }
          })
        });
    },
    exp(){
      window.open("http://8.138.58.18:7777/orders/export")
    },
    changeStatus(row){
      this.request.put("/orders/changeStatus/"+row.id).then(res =>{
        if(res.code === '200'){
          if(res.data === "起售"){
            this.$message.success('套餐起售成功!')
            this.load()
          }else{
            this.$message.success('套餐停售成功!')
            this.load()
          }
        }else{
          this.$message.error('修改套餐销售状态失败!')
          this.load()
        }
      })
      // console.log('row1',row)
      // console.log('this.tableData',this.tableData)
      // console.log('status1',row.status);
      // if(row.status === "起售"){
      //   console.log('1');
      //   // this.form.status = "停售"
      //   console.log('this.tableData[0].status',this.tableData[0].status);
      //   this.tableData[0].status = "停售"
      // }else{
      //   console.log('2');
      //   this.form.status = "起售"
      // }
      // this.load()
      // console.log('row2',row);
      // console.log('status2',row.status);
    }
  }
};
</script>

<style>
.headerBg5 {
  background-color: rgb(183, 247, 190) !important;
}
</style>